/* components */
import BizTableTask from '@src/component/business/BizTable/BizTableTask.tsx'
import formatColumn from 'packages/BaseTableCell/formatColumn'
import TaskSearchPanel from '@src/modules/task/components/list/TaskSearchPanel.vue'
/* enum */
import ComponentNameEnum from '@model/enum/ComponentNameEnum'
import { TaskFieldNameMappingEnum } from '@model/enum/FieldMappingEnum'
/* entity */
import Task from '@model/entity/Task'
import Field from '@model/entity/Field'
/* model */
import Page from '@model/Page'
/* render */
import BizListViewTaskRender from '@src/component/business/BizListView/Impl/BizListViewTask/BizListViewTaskRender.tsx'
/* service */
import { 
  getFieldName,
  isAttachmentFiled, 
  isNotSupportedDisplayField, 
  isSearchField, 
  isSystemFiled, 
  isTaskNoField, 
  mergeFields,
} from '@service/FieldService'
/* util */
import _ from 'lodash'
import { fieldArrayToObject } from '@src/util/array'
/* vue */
import { Component, Watch } from 'vue-property-decorator'

enum BizListViewTaskWatchPropertyEnum {
  TaskFields = 'taskFields'
}

@Component({ 
  name: ComponentNameEnum.BizListViewTask,
  components: {
    BizTableTask,
    [TaskSearchPanel.name]: TaskSearchPanel
  }
})
export default class BizListViewTask<T = Task, P = Page> extends BizListViewTaskRender<T, P> {
  
  /** 
   * @description 高级搜索列字段
  */
  get advanceSearchColumn(): Field[] {
    const mergeFieldNames = [TaskFieldNameMappingEnum.ServiceContent, TaskFieldNameMappingEnum.ServiceType, TaskFieldNameMappingEnum.Level]
    const taskFields = this.taskFields.filter(field => !isSystemFiled(field) && field.isSearch === 1)
    const cloneTaskFields = _.cloneDeep([...taskFields])
    cloneTaskFields.map(field => {
      // TODO:数坤单独做处理，因为数坤的客户关联字段-区域写死了fieldName为area，和工单系统字段area重复了，在高级搜索时会当成地址去搜索，所以改一下fieldName
      if (field.fieldName == 'area' && field.setting?.isShuKun && field.formType == 'relationCustomer') {
        field.fieldName = 'shuKunArea'
        field.field = 'shuKunArea'
      }
      return field
    })
    return (
      this.advancedSearchColumnsFilterHandler(
        [
          ...mergeFields(this.getAdvancedFields(this), cloneTaskFields, mergeFieldNames),
          ...this.taskReceiptFields.filter(taskReceiptField => {
            // fix:19992
            // 只显示回执自定义高级搜索字段
            return taskReceiptField.isSystem == 0 && taskReceiptField.isSearch == 1
          })
        ]
      )
    )
  }
  
  /**
   * @description: 工单字段列表变化事件
   * @param {Field} newValue 变化后的新工单字段列表
   * @param {Field} oldValue 变化前的旧工单字段列表
   * @return {void} 
   */  
  @Watch(BizListViewTaskWatchPropertyEnum.TaskFields)
  public onTaskFieldsChangedHandler(newValue: Field[], oldValue: Field[]): void {
    this.taskFieldsMap = fieldArrayToObject<Field>(newValue)
    // 构建列数据
    this.columns = [
      ...this.getColumnFields(this),
      ...this.taskFields.filter(field => {
        // 是否为系统字段
        const isSystem = isSystemFiled(field)
        // 是否为不支持显示的字段
        const isNotSupportedDisplay = isNotSupportedDisplayField(field)
        
        return !isSystem && !isNotSupportedDisplay
      })
    ].map(field => {
      return {
        ...formatColumn(field),
        field: getFieldName(field)
      }
    })

    // 合并本地缓存列数据至当前实例的列数据
    this.mergeLocalStorageColumnsToColumns()
  }
}

